package com.hoshiicloud.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.common.utils.tree.GoodsCategoryWithGoodNumTree;
import com.hoshiicloud.common.utils.tree.Menu;
import com.hoshiicloud.goods.entity.goods.GoodsCategory;
import com.hoshiicloud.goods.param.goods.GoodsCategoryAddParam;
import com.hoshiicloud.goods.param.goods.GoodsCategoryQueryParam;
import com.hoshiicloud.goods.param.goods.GoodsCategoryStatusUpdateParam;
import com.hoshiicloud.goods.param.goods.GoodsCategoryUpdateParam;
import com.hoshiicloud.goods.service.GoodsCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 商品类目表 控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-06-04
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/goods/goods-category")
@Api(value = "商品类目", tags = {"商品类目"})
public class GoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;

    @ApiOperation(value = "分页查询商品类目表")
    @PostMapping("/getPage")
    public Response<Page<GoodsCategory>> page(@RequestBody @Valid GoodsCategoryQueryParam param) {
        try {
            QueryWrapper<GoodsCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(GoodsCategory::getCompanyId,param.getCompanyId())
                    .eq((param.getParentId() != null), GoodsCategory::getParentId, param.getParentId())
                    .eq(StringUtils.isNotBlank(param.getName()), GoodsCategory::getName, param.getName())
                    .eq((param.getLevel() != null), GoodsCategory::getLevel, param.getLevel())
                    .eq( GoodsCategory::getDeleted,0);
            Page<GoodsCategory> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = "GoodsCategoryQueryParam{"+param.toString() + "},Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(goodsCategoryService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询商品类目表异常", e);
            return Response.failed("分页查询商品类目表异常");
        }
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询商品类目表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response<GoodsCategory> queryById(@PathVariable("id") Long id) {
        return Response.data(goodsCategoryService.getById(id));
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增商品类目表")
    public Response<Boolean> save(@RequestBody GoodsCategoryAddParam param) {
        // Bean数据转换
        GoodsCategory entity;
        try {
            entity = BeanUtils.covertType(param, GoodsCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsCategoryService.save(entity));
    }

    @PostMapping("/saveEntity")
    @ApiOperation(value = "新增商品类目表,实体入仓")
    public Response<Long> saveEntity(@RequestBody GoodsCategory entity) {
        goodsCategoryService.save(entity);
        return Response.data(entity.getId());
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改商品类目表")
    public Response<Boolean> update(@RequestBody GoodsCategoryUpdateParam param) {
        // Bean数据转换
        GoodsCategory entity;
        try {
            entity = BeanUtils.covertType(param, GoodsCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsCategoryService.updateById(entity));
    }

    @PutMapping("/updateByIdEntity")
    @ApiOperation(value = "修改商品类目表,实体入仓")
    public Response<Boolean> updateEntity(@RequestBody GoodsCategory entity) {
        return Response.data(goodsCategoryService.updateById(entity));
    }


    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除商品类目表")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response delete(@PathVariable("id") Long id) {
        return Response.data(goodsCategoryService.deleteById(id));
    }


    @ApiOperation(value = "根据id集合查询商品类目表")
    @GetMapping("/getByIds")
    public Response getByIds(@RequestParam List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Response.failed("id不能为空");
        }
        ids.sort(null);
        return Response.data(goodsCategoryService.getByIds(ids, StringUtils.join(ids, "")));
    }

    @ApiOperation(value = "商品类目查询,树状数据(不包含禁用)")
    @GetMapping("/getCategorys")
    public Response<List<Menu>> getCategorys(@RequestParam("companyId") Long companyId) {
        return Response.data(goodsCategoryService.getCategorys(companyId));
    }

    @ApiOperation(value = "商品类目查询带商品数量,树状数据")
    @GetMapping("/getCategorys2")
    public Response<List<GoodsCategoryWithGoodNumTree>> getCategorys2(@RequestParam("companyId") Long companyId) {
        return Response.data(goodsCategoryService.getCategorysWithGoodsNum(companyId));
    }

    @ApiOperation(value = "商品类目查询,树状数据(含禁用)")
    @GetMapping("/getCategorys3")
    public Response<List<Menu>> getCategorys3(@RequestParam("companyId") Long companyId) {
        return Response.data(goodsCategoryService.getCategorys3(companyId));
    }



    @ApiOperation(value = "修改类目的启用禁用状态")
    @PutMapping("/updateStatus")
    public Response<Boolean> updateStatus(@RequestBody GoodsCategoryStatusUpdateParam param) {
        GoodsCategory goodsCategory;
        try {
            goodsCategory = BeanUtils.covertType(param,GoodsCategory.class);
        } catch (Exception e) {
            log.error("Bean转换异常", e);
            return Response.failed("Bean转换异常");
        }
        return Response.data(goodsCategoryService.updateStatus(goodsCategory));
    }

    @ApiOperation(value = "根据三级类目id查询类目名称，名称以 > 的形式分隔")
    @GetMapping("/getCategoryContext")
    public Response getCategoryContext(@RequestParam("categoryContextQueryStr") String categoryContextQueryStr) {
        return Response.data(goodsCategoryService.selectcategoryContext(categoryContextQueryStr));
    }

}
